package user_information;

import java.io.IOException;



import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.catalina.core.ApplicationPart;
import org.json.JSONObject;

import DB.DB_connection_pool;

@MultipartConfig()
public class User_data_edit extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String path = request.getServletContext().getRealPath("/photo");
		String photo_url="";
		String user_account = request.getSession().getAttribute("user_account").toString();
		Part p = request.getPart("user_photo_file");
		
			ApplicationPart ap = (ApplicationPart)p;
			String filename = ap.getSubmittedFileName();
			
			if(filename!=null){
				int last = filename.lastIndexOf(".");
				String file_suffix = filename.substring(last, filename.length());   //鏂囦欢鍚庣紑
			photo_url = path+user_account+file_suffix;
			ap.write(photo_url);     //瀛樺叆鏂囦欢澶�
			photo_url = "photo/"+user_account+file_suffix;
		}
		
		String nickname = request.getParameter("nickname");
		String hobby = request.getParameter("hobby");
		String address = request.getParameter("address");
		String birthday = request.getParameter("birthday");
		String sex = request.getParameter("sex");
		String autograph = request.getParameter("personal_profile");
		String qq = request.getParameter("qq");
		
		String update_sql = "";
		if(filename!=null){
			if((!birthday.equals(""))&&birthday!=null){
		update_sql = "update user_data set user_photo_path = '"+photo_url+"' , user_introduction = '"+autograph+"' , user_sex = '"+sex+"' , user_hobby = '"+hobby+"' , user_nickname = '"+nickname+"' , user_birthday = '"+birthday+"' , user_birth_place = '"+address+"' , user_qq = '"+qq+"' where user_account='"+user_account+"'";
			}else{
		update_sql = "update user_data set user_photo_path = '"+photo_url+"' , user_introduction = '"+autograph+"' , user_sex = '"+sex+"' , user_hobby = '"+hobby+"' , user_nickname = '"+nickname+"' , user_birth_place = '"+address+"' , user_qq = '"+qq+"' where user_account='"+user_account+"'";
			}
			}else{
				if((!birthday.equals(""))&&birthday!=null){
		update_sql = "update user_data set user_introduction = '"+autograph+"' , user_sex = '"+sex+"' , user_hobby = '"+hobby+"' , user_nickname = '"+nickname+"' , user_birthday = '"+birthday+"' , user_birth_place = '"+address+"' , user_qq = '"+qq+"' where user_account='"+user_account+"'";
				}else{
		update_sql = "update user_data set user_introduction = '"+autograph+"' , user_sex = '"+sex+"' , user_hobby = '"+hobby+"' , user_nickname = '"+nickname+"' , user_birth_place = '"+address+"' , user_qq = '"+qq+"' where user_account='"+user_account+"'";
				}
				}
		JSONObject jsonObj = new JSONObject();
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_update = conn.prepareStatement(update_sql);
			ps_update.executeUpdate();
			
			ps_update.close();
			conn.close();
			jsonObj.put("result", true);
		} catch (SQLException e) {
			jsonObj.put("result", false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jsonObj.toString());
	}

}
